import { loadErrorHandler } from './index.type';

/*
 * @Descripttion:
 * @LastEditors: chen.long
 * @LastEditTime: 2024-02-24 13:22:44
 */
export type ScriptAttributes = {
  [key: string]: string | boolean; // 所有属性都可以是字符串或布尔值类型
};

/** 脚本对象 */
export interface ScriptBaseObject {
  /** 脚本地址，内联为空 */
  src?: string;
  /** 脚本是否为async执行 */
  async?: boolean;
  /** 脚本是否为defer执行 */
  defer?: boolean;
  /** 脚本是否为module模块 */
  module?: boolean;
  /** 脚本是否设置crossorigin */
  crossorigin?: boolean;
  /** 脚本crossorigin的类型 */
  crossoriginType?: 'anonymous' | 'use-credentials' | '';
  /** 脚本正则匹配属性 */
  attrs?: ScriptAttributes;
}
export type ScriptObject = ScriptBaseObject & {
  /** 内联script的代码 */
  content?: string;
  /** 忽略，子应用自行请求 */
  ignore?: boolean;
  /** 子应用加载完毕事件 */
  onload?: Function;
};

/** 样式对象 */
export interface StyleObject {
  /** 样式地址， 内联为空 */
  src?: string;
  /** 样式代码 */
  content?: string;
  /** 忽略，子应用自行请求 */
  ignore?: boolean;
}

export interface TemplateResult {
  template: string;
  scripts: ScriptObject[];
  styles: StyleObject[];
  entry: string | ScriptObject;
}

export type StyleResultList = {
  src: string;
  contentPromise: Promise<string>;
  ignore?: boolean;
}[];

export type ScriptResultList = (ScriptBaseObject & {
  contentPromise: Promise<string>;
})[];

export interface htmlParseResult {
  template: string;

  assetPublicPath: string;

  getExternalScripts(): ScriptResultList;

  getExternalStyleSheets(): StyleResultList;
}

export type ImportEntryOpts = {
  loadError?: loadErrorHandler;
};
